      1  import java.util.PriorityQueue;

      2

      3  public class Simulation

      4  {

      5     private PriorityQueue<Event> eventQueue;

      6     private double currentTime;

      7

      8     /**

      9        Constructs a discrete event simulation.

     10     */

     11     public Simulation()

     12     {

     13        eventQueue = new PriorityQueue<Event>();

     14     }

     15

     16     public double getCurrentTime()

     17     {

     18        return currentTime;

     19     }

     20

     21     /**

     22        Compute exponentially distributed random numbers.

     23        @param mean the mean of the number sequence

     24        @return a random number

     25     */

     26     public static double expdist(double mean)

     27     {

     28        return -mean * Math.log(1 - Math.random());

     29     }

     30

     31     /**

     32        Adds an event to the event queue.

     33        @param evt the event to add

     34     */

     35     public void addEvent(Event evt)

     36     {

     37        eventQueue.add(evt);

     38     }

     39

     40     /**

     41        Displays intermediate results after each event.

     42     */

     43     public void display() {}

     44

     45     /**

     46        Displays summary results after the end of the simulation.

     47     */

     48     public void displaySummary() {}

     49

     50     public void run(double startTime, double endTime)

     51     {

     52        currentTime = startTime;

     53

     54        while (eventQueue.size() > 0 && currentTime <= endTime)

     55        {

     56           Event event = eventQueue.remove();

     57           currentTime = event.getTime();

     58           event.process(this);

     59           display();

     60        }

     61        displaySummary();

     62     }

     63  }